/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ChatWindow.java
 *
 * Created on Oct 31, 2011, 5:02:58 PM
 */
package chat.client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author cibervn
 */
public class ChatWindow extends javax.swing.JPanel {
    private PrintWriter pw;
    private BufferedReader br;
    final private StringBuilder sb = new StringBuilder();

    /** Creates new form ChatWindow */
    public ChatWindow(Socket socket) {
        initComponents();
        try {
            pw = new PrintWriter(socket.getOutputStream());
            br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            new Thread(new Runnable() {

                public void run() {
                    while (true) {
                        try {
                            String line = br.readLine();
                            //line= line.trim();
                            if (line != null) {
                                sb.append('\n');
                                sb.append(line);
                                txaConversation.setText(sb.toString());
                            }
                        } catch (IOException ex) {
                            Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
                        }
                    }
                }
            }).start();
        } catch (IOException ex) {
            Logger.getLogger(ChatWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        txaConversation = new javax.swing.JTextArea();
        pnAction = new javax.swing.JPanel();
        txtMessage = new javax.swing.JTextField();
        btnSend = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());

        txaConversation.setColumns(20);
        txaConversation.setRows(5);
        jScrollPane1.setViewportView(txaConversation);

        add(jScrollPane1, java.awt.BorderLayout.CENTER);

        pnAction.setLayout(new java.awt.FlowLayout(java.awt.FlowLayout.RIGHT));

        txtMessage.setColumns(35);
        pnAction.add(txtMessage);

        btnSend.setText("Send");
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });
        pnAction.add(btnSend);

        add(pnAction, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        pw.println(txtMessage.getText());
        pw.flush();
        txtMessage.setText("");
    }//GEN-LAST:event_btnSendActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnSend;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JPanel pnAction;
    private javax.swing.JTextArea txaConversation;
    private javax.swing.JTextField txtMessage;
    // End of variables declaration//GEN-END:variables
}
